import numpy as np
from scipy.signal import convolve
import matplotlib.pyplot as plt
import matplotlib

# Define the Barker codes to be compared
barker1 = np.array([1, 1, 1, 1, 1, -1, -1, 1, 1, -
                   1, 1, -1, 1])  # your Barker code
barker2 = np.array([1, 1, 1, -1, -1, -1, 1, -1, -1, 1, -1, 1, 1])
barker3 = np.array([1, 1, -1, 1, -1, 1, 1, -1, -1, -1, 1, -1, 1])
barker4 = np.array([1, 1, 1, -1, -1, 1, -1, 1, -1, -1, 1, -1, 1])

# Generate a test signal
signal = np.random.randn(1000)

# Apply each Barker code to the test signal and measure the SNR
snr1 = 0
snr2 = 0
snr3 = 0
snr4 = 0

for i in range(10):
    compressed_signal1 = convolve(signal, barker1, mode='same')
    snr1 += 10*np.log10(np.sum(np.abs(compressed_signal1)
                        ** 2) / np.sum(np.abs(signal)**2))

    compressed_signal2 = convolve(signal, barker2, mode='same')
    snr2 += 10*np.log10(np.sum(np.abs(compressed_signal2)
                        ** 2) / np.sum(np.abs(signal)**2))

    compressed_signal3 = convolve(signal, barker3, mode='same')
    snr3 += 10*np.log10(np.sum(np.abs(compressed_signal3)
                        ** 2) / np.sum(np.abs(signal)**2))

    compressed_signal4 = convolve(signal, barker4, mode='same')
    snr4 += 10*np.log10(np.sum(np.abs(compressed_signal4)
                        ** 2) / np.sum(np.abs(signal)**2))

# Calculate the average SNR for each Barker code
snr1 /= 10
snr2 /= 10
snr3 /= 10
snr4 /= 10

# Plot the results
labels = ['Barker1', 'Barker2', 'Barker3', 'Barker4']
snrs = [snr1, snr2, snr3, snr4]

plt.bar(labels, snrs)
plt.xlabel('Barker code')
plt.ylabel('SNR (dB)')
plt.title('Comparison of Barker codes')
plt.show()
